package skinfactory
{
	import caurina.transitions.Tweener;
	
	import events.ButtonEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class SkinButton extends Sprite
	{
		private var arrBtnImg: Array;
		protected var btnArea: Sprite;
		
		//Tweener를 적용 유무 
		private var _effect:Boolean;
		
		public function SkinButton( tag: String, effect: Boolean = true )
		{
			/** arrbtnImg[0] : Normal Img
			 *  arrbtnImg[1] : Over Img
			 *  arrbtnImg[2] : Down Img
			 **/ 
			_effect = effect;
			arrBtnImg = SkinFactory.getInstance().getButtonImg( tag );
			
			this.initDisplay();
		}
		private function initDisplay(): void
		{
			btnArea = new Sprite();
			btnArea.graphics.beginFill( 0xFF0000, 0 );
			btnArea.graphics.drawRect(0, 0, arrBtnImg[0].width, arrBtnImg[0].height );
			btnArea.graphics.endFill();
			
			btnArea.addEventListener( MouseEvent.MOUSE_OVER, onMouseEventHandler );
			btnArea.addEventListener( MouseEvent.MOUSE_OUT, onMouseEventHandler );
			btnArea.addEventListener( MouseEvent.MOUSE_DOWN, onMouseEventHandler );
			btnArea.addEventListener( MouseEvent.CLICK, onMouseEventHandler );
			
			btnArea.buttonMode = true;
			
			arrBtnImg[1].alpha = 0;
			arrBtnImg[2].visible = false;
			
			this.setPosition();
			this.registerObj();
		}
		protected function setPosition():void
		{
			
		}
		protected function registerObj():void
		{
			this.addChild( arrBtnImg[0] );
			this.addChild( arrBtnImg[1] );
			this.addChild( arrBtnImg[2] );
			this.addChild( btnArea );
		}
		private function onMouseEventHandler( e: MouseEvent ): void
		{
			arrBtnImg[0].visible = true;
			arrBtnImg[1].visible = true;
			arrBtnImg[2].visible = false;
			if( _effect )
			{
				switch( e.type )
				{
					case MouseEvent.MOUSE_OVER:
						Tweener.addTween( arrBtnImg[0], { alpha:0, time: 0.3, transition:'easeOutCubic' } );
						Tweener.addTween( arrBtnImg[1], { alpha:1, time: 0.3, transition:'easeOutCubic' } ); 
						break;
					case MouseEvent.MOUSE_OUT:
						Tweener.addTween( arrBtnImg[1], { alpha:0, time: 0.3, transition:'easeOutCubic' } );
						Tweener.addTween( arrBtnImg[0], { alpha:1, time: 0.3, transition:'easeOutCubic' } );
						break;
					case MouseEvent.MOUSE_DOWN:
						arrBtnImg[0].visible = false;
						arrBtnImg[1].visible = false;
						arrBtnImg[2].visible = true;
						break;
					case MouseEvent.CLICK:
						this.dispatchEvent( new ButtonEvent( ButtonEvent.CLICK ) );
						break;
				}				
			}
			else
			{
				switch( e.type )
				{
					case MouseEvent.MOUSE_OVER:
						arrBtnImg[1].alpha = 1;
						break;
					case MouseEvent.MOUSE_OUT:
						arrBtnImg[1].alpha = 0;
						break;
					case MouseEvent.MOUSE_DOWN:
						arrBtnImg[0].visible = false;
						arrBtnImg[1].visible = false;
						arrBtnImg[2].visible = true;
						break;
				}	
			}
			
		}
		public function set effect( value: Boolean ): void
		{
			_effect = value;
		}
		override public function set width(value:Number):void
		{
			for ( var i: int = 0; i < arrBtnImg.length; i++ )
			{
				arrBtnImg[i].width = value;
			}
			btnArea.width = value; 
			
			this.setPosition();
		}
		override public function set height(value:Number):void
		{
			for ( var i: int = 0; i < arrBtnImg.length; i++ )
			{
				arrBtnImg[i].height = value;
			}
			btnArea.height = value;
			
			this.setPosition();
		} 
		public function set keepUpButton( value: Boolean ): void
		{
			if( value )
			{
				Tweener.addTween( arrBtnImg[0], { alpha:0, time: 0.3, transition:'easeOutCubic' } );
				Tweener.addTween( arrBtnImg[1], { alpha:1, time: 0.3, transition:'easeOutCubic' } );
						
				//arrBtnImg[0].visible = false;
				//arrBtnImg[1].visible = false;
				//arrBtnImg[2].visible = true;
				btnArea.removeEventListener( MouseEvent.MOUSE_OVER, onMouseEventHandler );
				btnArea.removeEventListener( MouseEvent.MOUSE_OUT, onMouseEventHandler );
				btnArea.removeEventListener( MouseEvent.MOUSE_DOWN, onMouseEventHandler );
				btnArea.removeEventListener( MouseEvent.CLICK, onMouseEventHandler );
				btnArea.buttonMode = false;
			}
			else
			{
				Tweener.addTween( arrBtnImg[1], { alpha:0, time: 0.3, transition:'easeOutCubic' } );
						Tweener.addTween( arrBtnImg[0], { alpha:1, time: 0.3, transition:'easeOutCubic' } );
				//arrBtnImg[0].visible = true;
				//arrBtnImg[1].visible = false;
				//arrBtnImg[2].visible = false;
				btnArea.addEventListener( MouseEvent.MOUSE_OVER, onMouseEventHandler );
				btnArea.addEventListener( MouseEvent.MOUSE_OUT, onMouseEventHandler );
				btnArea.addEventListener( MouseEvent.MOUSE_DOWN, onMouseEventHandler );
				btnArea.addEventListener( MouseEvent.CLICK, onMouseEventHandler );
			}
		}
		override public function set buttonMode(value:Boolean):void
		{
			if( value )
			{
				arrBtnImg[0].alpha = 1;
				btnArea.addEventListener( MouseEvent.MOUSE_OVER, onMouseEventHandler );
				btnArea.addEventListener( MouseEvent.MOUSE_OUT, onMouseEventHandler );
				btnArea.addEventListener( MouseEvent.MOUSE_DOWN, onMouseEventHandler );
				btnArea.addEventListener( MouseEvent.CLICK, onMouseEventHandler );
				btnArea.buttonMode = true;
			}
			else
			{
				arrBtnImg[0].alpha = 0.5;
				btnArea.removeEventListener( MouseEvent.MOUSE_OVER, onMouseEventHandler );
				btnArea.removeEventListener( MouseEvent.MOUSE_OUT, onMouseEventHandler );
				btnArea.removeEventListener( MouseEvent.MOUSE_DOWN, onMouseEventHandler );
				btnArea.removeEventListener( MouseEvent.CLICK, onMouseEventHandler );
				btnArea.buttonMode = false;
			}
		} 
	}
}